
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>结合Python保存Excel · MLSQL Engine 文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../styles/image.css">
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="resource.html" />
    
    
    <link rel="prev" href="read_excel.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    MLSQL Engine产品简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../howtouse/">
            
                <a href="../howtouse/">
            
                    
                    MLSQL Engine基本安装
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../howtouse/configuration.html">
            
                <a href="../howtouse/configuration.html">
            
                    
                    启动参数详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../howtouse/deploy.html">
            
                <a href="../howtouse/deploy.html">
            
                    
                    Local
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../howtouse/k8s_deploy.html">
            
                <a href="../howtouse/k8s_deploy.html">
            
                    
                    K8s部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../howtouse/yarn_deploy.html">
            
                <a href="../howtouse/yarn_deploy.html">
            
                    
                    Yarn部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../security/README.md">
            
                <span>
            
                    
                    MLSQL Engine安全加固
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../security/token_control.html">
            
                <a href="../security/token_control.html">
            
                    
                    接口访问Token设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../security/custom_control.html">
            
                <a href="../security/custom_control.html">
            
                    
                    自定义接口访问策略
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../auth/">
            
                <a href="../auth/">
            
                    
                    MLSQL Engine数据访问控制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" >
            
                <span>
            
                    
                    解析时表权限
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" >
            
                <span>
            
                    
                    运行时表权限表以及列权限
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" >
            
                <span>
            
                    
                    客户端开发
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" >
            
                <span>
            
                    
                    服务端开发
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../api/README.md">
            
                <span>
            
                    
                    MLSQL Engine Rest接口详解
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../api/run-script.html">
            
                <a href="../api/run-script.html">
            
                    
                    /run/script
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="./">
            
                <a href="./">
            
                    
                    MLSQL Python支持
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="env.html">
            
                <a href="env.html">
            
                    
                    环境依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="etl.html">
            
                <a href="etl.html">
            
                    
                    数据处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="train.html">
            
                <a href="train.html">
            
                    
                    模型训练
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="pyjava.html">
            
                <a href="pyjava.html">
            
                    
                    PyJava API简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="read_excel.html">
            
                <a href="read_excel.html">
            
                    
                    结合Python读取Excel
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.6" data-path="write_excel.html">
            
                <a href="write_excel.html">
            
                    
                    结合Python保存Excel
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="resource.html">
            
                <a href="resource.html">
            
                    
                    K8s下的Python资源限制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="datamode.html">
            
                <a href="datamode.html">
            
                    
                    dataMode 详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="py_paralell.html">
            
                <a href="py_paralell.html">
            
                    
                    Python并行度你所需要知道的
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../plugin/">
            
                <a href="../plugin/">
            
                    
                    插件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../plugin/online_install.html">
            
                <a href="../plugin/online_install.html">
            
                    
                    网络安装插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../plugin/offline_install.html">
            
                <a href="../plugin/offline_install.html">
            
                    
                    离线安装插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >结合Python保存Excel</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7ED3;&#x5408;python&#x4FDD;&#x5B58;excel">&#x7ED3;&#x5408;Python&#x4FDD;&#x5B58;Excel</h1>
<p>MLSQL 2.0.1-SNAPSHOT/2.0.1 &#x4EE5;&#x53CA;&#x4EE5;&#x4E0A;&#x7248;&#x672C;&#x53EF;&#x7528;</p>
<blockquote>
<p>MLSQL &#x63D0;&#x4F9B;&#x4E86; <a href="https://github.com/allwefantasy/mlsql-plugins/tree/master/mlsql-excel" target="_blank">excel&#x63D2;&#x4EF6;</a>&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x7528;&#x6237;&#x8BFB;&#x5199;Excel&#x3002;&#x66F4;&#x591A;&#x8BE6;&#x60C5;&#x53C2;&#x8003;<a href="http://docs.mlsql.tech/mlsql-engine/plugin/" target="_blank">&#x63D2;&#x4EF6;</a></p>
</blockquote>
<h2 id="&#x52A0;&#x8F7D;&#x6570;&#x636E;">&#x52A0;&#x8F7D;&#x6570;&#x636E;</h2>
<p>&#x9996;&#x5148;&#x52A0;&#x8F7D;&#x6570;&#x636E;</p>
<pre><code class="lang-sql"><span class="hljs-keyword">load</span> delta.<span class="hljs-string">`public.simpleData`</span> <span class="hljs-keyword">as</span> simpleDataTemp;
<span class="hljs-comment">-- simpleDataTemp&#x8F6C;&#x5316;&#x6210;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5206;&#x7247;&#x6570;&#x636E;&#x7684;&#x65B0;&#x8868;simpleData; </span>
!tableRepartition _ -i simpleDataTemp -num 1 -o simpleData;
</code></pre>
<p>&#x5982;&#x4E0B;&#xFF1A;</p>
<p></p><div class="image-wrapper python-write_excel"><a href="http://docs.mlsql.tech/upload_images/4b50b955-3efe-4ade-beee-dbbac8cf4c3b.png" data-lightbox="418d245f-0a05-49e3-828e-618fdb16be92" data-title="" target="_blank"><img src="http://docs.mlsql.tech/upload_images/4b50b955-3efe-4ade-beee-dbbac8cf4c3b.png" alt=""></a></div><p></p>
<h2 id="ray&#x914D;&#x7F6E;">Ray&#x914D;&#x7F6E;</h2>
<pre><code>-- python&#x5904;&#x7406;&#x5B8C;&#x540E;&#x7684;&#x6570;&#x636E;&#x7684;schema
!python conf &quot;schema=st(field(file,binary))&quot;;
-- Python&#x8DD1;&#x5728;driver&#x7AEF;&#xFF0C;
!python conf &quot;runIn=driver&quot;;
-- &#x4E0D;&#x9700;&#x8981;&#x4ECE;Ray&#x96C6;&#x7FA4;&#x83B7;&#x53D6;&#x6570;&#x636E;&#xFF0C;&#x9700;&#x8981;&#x5C06;dataMode&#x8BBE;&#x7F6E;&#x4E3A;model.
!python conf &quot;dataMode=model&quot;;
</code></pre><h2 id="&#x4F7F;&#x7528;python&#x4EE3;&#x7801;&#x5904;&#x7406;&#x6570;&#x636E;&#xFF0C;&#x751F;&#x6210;excel">&#x4F7F;&#x7528;Python&#x4EE3;&#x7801;&#x5904;&#x7406;&#x6570;&#x636E;&#xFF0C;&#x751F;&#x6210;Excel</h2>
<pre><code class="lang-python">!ray on simpleData <span class="hljs-string">&apos;&apos;&apos;

import io
import ray
from pyjava.api.mlsql import RayContext
import pandas as pd

ray_context = RayContext.connect(globals(),None)

rows = [item for item in RayContext.collect_from(ray_context.data_servers())]

df = pd.DataFrame(data=rows)
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine=&apos;xlsxwriter&apos;)
df.to_excel(writer)
writer.save()
xlsx_data = output.getvalue()
context.build_result([{&quot;file&quot;:xlsx_data}])

&apos;&apos;&apos;</span> named excel_data;
</code></pre>
<p>&#x7B80;&#x5355;&#x89E3;&#x91CA;&#x4E0B;&#x3002;</p>
<pre><code class="lang-python">ray_context = RayContext.connect(globals(),<span class="hljs-keyword">None</span>)
</code></pre>
<p>&#x83B7;&#x53D6;ray_context,&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;None&#x8868;&#x793A;&#x4E0D;&#x9002;&#x7528;Ray&#x96C6;&#x7FA4;&#x3002;</p>
<pre><code class="lang-python">rows = [item <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> RayContext.collect_from(ray_context.data_servers())]

df = pd.DataFrame(data=rows)
</code></pre>
<p>&#x83B7;&#x53D6;&#x8868;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x5176;&#x8F6C;&#x5316;&#x4E3A;Pandas DataFrame.</p>
<p>&#x5269;&#x4E0B;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x5177;&#x4F53;&#x5C06;&#x6570;&#x636E;&#x8F6C;&#x5316;&#x4E3A;excel&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x7EC4;&#x3002;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python">context.build_result([{<span class="hljs-string">&quot;file&quot;</span>:xlsx_data}])
</code></pre>
<p>&#x8F93;&#x51FA;&#x5230; excel_data &#x8868;&#x4E2D;&#x3002;</p>
<p>Pandas&#x5BF9;Excel&#x66F4;&#x591A;&#x64CD;&#x4F5C;&#x53C2;&#x8003;&#x8FD9;&#x91CC;<a href="https://xlsxwriter.readthedocs.io/working_with_pandas.html" target="_blank">working_with_pandas</a></p>
<h2 id="&#x4FDD;&#x5B58;&#x6587;&#x4EF6;">&#x4FDD;&#x5B58;&#x6587;&#x4EF6;</h2>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;<code>!saveFile</code> &#x5C06;excel&#x4FDD;&#x5B58;&#x8D77;&#x6765;.</p>
<pre><code class="lang-shell">!saveFile _ -i excel_data -o /tmp/wow.xlsx;
</code></pre>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;  <code>_</code> &#x8868;&#x793A;&#x8FD9;&#x91CC;&#x4F1A;&#x4F7F;&#x7528;&#x547D;&#x4EE4;&#x884C;&#x6A21;&#x5F0F;&#x89E3;&#x6790;&#x3002; <code>-i</code> &#x8868;&#x793A;&#x8F93;&#x5165;&#x7684;&#x8868;&#xFF0C; <code>-o</code> &#x8868;&#x793A;&#x8F93;&#x51FA;&#x8DEF;&#x5F84;&#x3002;</p>
<h2 id="&#x9644;&#x5F55;">&#x9644;&#x5F55;</h2>
<p>&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-sql">load delta.`public.simpleData` as simpleData;

-- !pyInclude &quot;excel.save.py&quot; named save_excel;

-- ray &#x914D;&#x7F6E;
!python env &quot;PYTHON_ENV=source activate dev&quot;;
!python conf &quot;schema=st(field(file,binary))&quot;;
!python conf &quot;runIn=driver&quot;;
!python conf &quot;dataMode=model&quot;;

!ray on simpleData &apos;&apos;&apos;

import io
import ray
from pyjava.api.mlsql import RayContext
import pandas as pd

ray_context = RayContext.connect(globals(),None)

rows = [item for item in RayContext.collect_from(ray_context.data_servers())]

df = pd.DataFrame(data=rows)
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine=&apos;xlsxwriter&apos;)
df.to_excel(writer)
writer.save()
xlsx_data = output.getvalue()
context.build_result([{&quot;file&quot;:xlsx_data}])

&apos;&apos;&apos; named excel_data;

!saveFile _ -i excel_data -o /tmp/wow.xlsx;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="read_excel.html" class="navigation navigation-prev " aria-label="Previous page: 结合Python读取Excel">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="resource.html" class="navigation navigation-next " aria-label="Next page: K8s下的Python资源限制">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"结合Python保存Excel","level":"1.6.6","depth":2,"next":{"title":"K8s下的Python资源限制","level":"1.6.7","depth":2,"path":"python/resource.md","ref":"python/resource.md","articles":[]},"previous":{"title":"结合Python读取Excel","level":"1.6.5","depth":2,"path":"python/read_excel.md","ref":"python/read_excel.md","articles":[]},"dir":"ltr"},"config":{"plugins":["image-class","lightbox"],"styles":{"website":"styles/image.css"},"pluginsConfig":{"image-class":{},"lightbox":{"jquery":true,"sameUuid":false},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MLSQL Engine 文档","gitbook":"*","description":"MLSQL Engine 官方文档"},"file":{"path":"python/write_excel.md","mtime":"2020-09-02T06:45:13.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-03T03:59:44.434Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

